home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / cbibcode.arc / _READ.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-05  |  860 b   |  34 lines

  1. /* _READ.C --- p. 650 */
  2. #include <stdio.h>
  3. #include <fcntl.h>
  4. #include <io.h>
  5. main()
  6. {
  7.     char fname[40], *p_fname;
  8.     char buffer[80];
  9.     int filehandle, bytes_read;
  10.     printf("Enter name of an existing file: ");
  11.     p_fname = gets(fname);
  12.                     /* Open the file using _open  */
  13.     if((filehandle = _open(p_fname, O_RDONLY)) == -1)
  14.     {
  15.         printf("Error opening file: %s\n", fname);
  16.         exit(0);
  17.     }
  18.     printf("File %s opened.\n", fname);
  19.                     /* Now read the first 80 bytes */
  20.     if((bytes_read = _read(filehandle, buffer, 80)) != -1)
  21.     {
  22.         printf("%d bytes read\n", bytes_read);
  23.         printf("The bytes read are :\n%s\n", buffer);
  24.     }
  25.     else
  26.         printf("Error reading from file: %s\n", fname);
  27.                         /* Now close the file */
  28.     if(_close(filehandle) != 0)
  29.     {
  30.         printf("Error closing file with _close\n");
  31.         exit(0);
  32.     }
  33.     printf("File %s closed.\n", fname);
  34. }